﻿using B.S.Ingredients.Infartructrue.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace B.S.Ingredients.Infartructrue.Impment
{
    /// <summary>
    /// 仓储
    /// </summary>
    /// <typeparam name="T">泛型</typeparam>
    public class BaseRepository<T>:IBaseRepository<T> where T : class
    {
        public BaseRepository(MyDbContext dbContext)
        {
            DbContext = dbContext;
        }

        public MyDbContext DbContext { get; }

        /// <summary>
        /// 返回所有值
        /// </summary>
        /// <returns>集合</returns>
        public  IQueryable<T> GetAll()
        {
            return DbContext.Set<T>();
        }
        
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="t">实体</param>
        /// <returns>受影响行数</returns>
        public async Task<int> CreateAsync(T t)
        {
            await DbContext.Set<T>().AddAsync(t);
            return await DbContext.SaveChangesAsync();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t">实体</param>
        /// <returns>受影响行数</returns>
        public async Task<int> UpdateAsync(List<T> t)
        {
             DbContext.Set<T>().UpdateRange(t);
            return await DbContext.SaveChangesAsync();

        }

        public async Task<int> Update(T t)
        {
            DbContext.Set<T>().Update(t);
            return await DbContext.SaveChangesAsync();
        }
       
    }
}
